package com.service;


import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;

@Service
public class MQService {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    public boolean sendMessage(String exchange, String key, Message msg)
    {
        //当消息到达队列的时候，会执行此代码
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                if (b)
                {
                    System.out.println("交换机存在");
                }
                else {
                    System.out.println("交换机不存在");
                }
            }
        });

        //如果没有正常到达队列，则回调此代码
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.out.println("发送队列失败");
            }
        });

        rabbitTemplate.convertAndSend(exchange,key,msg);

        return true;
    }




}
